package com.tjhk.java.tech;

public class ThreadLocalTest {
    //    private String name;
    private ThreadLocal<String> name = new ThreadLocal<>();
    private int age;

    public static void main(String[] args) {
        ThreadLocalTest test = new ThreadLocalTest();
        // 存在线程安全问题
//        for (int i = 0; i < 5; i++) {
//            new Thread(() ->{
//                test.setName(Thread.currentThread().getName() + "的数据");
//                System.out.println("==============");
//                System.out.println(Thread.currentThread().getName() + "---->" + test.getName());
//            },"t" + i).start();
//        }
//        // synchronized 关键字
//        for (int i = 0; i < 5; i++) {
//            new Thread(() ->{
//                synchronized (ThreadTest.class){
//                    test.setName(Thread.currentThread().getName() + "的数据");
//                    System.out.println("==============");
//                    System.out.println(Thread.currentThread().getName() + "---->" + test.getName());
//                }
//            },"st" + i).start();
//        }

        for (int i = 0; i < 5; i++) {
            new Thread(() ->{
                test.setName(Thread.currentThread().getName() + "的数据");
                System.out.println("==============");
                System.out.println(Thread.currentThread().getName() + "---->" + test.getName());

            },"tt" + i).start();
        }

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

//    public String getName() {
//        return name;
//    }
//
//    public void setName(String name) {
//        this.name = name;
//    }

    public String getName() {
        return name.get();
    }

    public void setName(String content) {
        name.set(content);
    }
}
